package top.lyc829.other.producerandcustomer;

import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.List;
import java.util.Queue;

public class Test {

    private Queue<Integer> list = new ArrayDeque<>();

    private class Producer extends Thread {
        @Override
        public void run() {
            super.run();
            synchronized (this) {
                int i = 0;
                while (true) {
//                    try {
//                        Thread.sleep(500);
//                    } catch (InterruptedException e) {
//                        e.printStackTrace();
//                    }
                    synchronized (list) {
                        while (list.size() >= 10) {
                            System.out.println("list full...");
                            try {
                                list.wait();
                            } catch (InterruptedException e) {
                                e.printStackTrace();
                                list.notifyAll();
                            }
                        }
                        System.out.println(list.toString());
                        list.offer(i++);
                        list.notifyAll();
                    }
                }


            }
        }
    }

    private class Customer extends Thread {
        @Override
        public void run() {
            super.run();
//            try {
//                Thread.sleep(1000);
//            } catch (InterruptedException e) {
//                e.printStackTrace();
//            }
            while (true) {
                synchronized (list) {
                    while (list.size() == 0) {
                        System.out.println("list empty...");
                        try {
                            list.wait();
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                            list.notifyAll();
                        }
                    }
                    System.out.println("list remove " + list.poll());

                    list.notifyAll();
                }
            }

        }
    }


    public static void main(String[] args) {

        Test test = new Test();

        Test.Producer producer = test.new Producer();
        Test.Customer customer = test.new Customer();

        producer.start();
        customer.start();



    }

}
